package com.rockxin.cat.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.rockxin.cat.R;
import com.rockxin.cat.adapter.RedPacketAdapter;
import com.rockxin.cat.base.BaseFragment;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.LazyBaseFragment;
import com.rockxin.cat.bean.RedPacketBean;
import com.rockxin.cat.event.RedPacketTypeEvent;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * 红包列表
 */
public class RedPacketListFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private Unbinder bind;
    private RedPacketAdapter mRedPacketAdapter;
    private List<RedPacketBean> redPacketBeanList;

    private int type;    //1=>'我的加息券列表',2=>'我的节日红包列表',3=>'我的8888现金红包活动列表'
    private int type2;   //1：未使用，2：已使用  3：已过期

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        redPacketBeanList = new ArrayList<>();
    }

    public RedPacketListFragment() {
    }

    public static RedPacketListFragment newInstance() {
        Bundle args = new Bundle();
        RedPacketListFragment fragment = new RedPacketListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.fragment_red_packet_list, container, false);
        bind = ButterKnife.bind(this, mView);
        initData();
        return mView;
    }

    private void getNetData() {
        CatNetWork catNetWork = new CatNetWork();
        catNetWork.myVoucher(type, type2, Constant.PREFERENCES.TOKEN, 1, new rx.Subscriber<HttpResult<List<RedPacketBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<RedPacketBean>> listHttpResult) {
                redPacketBeanList.addAll(listHttpResult.getData());
                mRedPacketAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRedPacketAdapter = new RedPacketAdapter(redPacketBeanList);
        recyclerView.setAdapter(mRedPacketAdapter);
        mRedPacketAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        bind.unbind();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        //Toast.makeText(mContext, "立即使用", Toast.LENGTH_SHORT).show();
    }

    @Subscriber
    public void getRedPacketType(RedPacketTypeEvent redPacketTypeEvent) {
        type2 = redPacketTypeEvent.getType2();
        type = redPacketTypeEvent.getType();
        getNetData();

    }

}
